package com.yh.ssm.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.config.entities.Parameterizable;
import com.yh.ssm.model.Page;
import com.yh.ssm.model.Product;
import com.yh.ssm.service.IProductService;

public class ProductAction extends ActionSupport implements ModelDriven<Object> {

	/**
	 * 商品参数信息
	 */
	private Product product;

	@Autowired
	/**
	 * 自动注入服务层业务
	 */
	private IProductService ips;

	public IProductService getIps() {
		return ips;
	}

	public void setIps(IProductService ips) {
		this.ips = ips;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	private String save;

	public String getSave() {
		return save;
	}

	public void setSave(String save) {
		this.save = save;
	}

	/**
	 * 接受分页数据信息
	 */
	private Page page;
	/**
	 * 接受分页参数
	 */
	private int pageSize;
	/**
	 * 接受当前页参数
	 */
	private int pageNum;
	/**
	 * 查询得到的最大商品价格
	 */
	private Integer maxprice;
	/**
	 * 上传图片的路径
	 */
	private File pa;
	/**
	 * 上传图片的名字
	 */
	private String paFileName;

	public File getPa() {
		return pa;
	}

	public void setPa(File pa) {
		this.pa = pa;
		
	}

	public String getPaFileName() {
		return paFileName;
	}

	public void setPaFileName(String paFileName) {
		this.paFileName = paFileName;
	}

	public Integer getMaxprice() {
		return maxprice;
	}

	public void setMaxprice(Integer maxprice) {
		this.maxprice = maxprice;
	}

	public Integer getMinprice() {
		return minprice;
	}

	public void setMinprice(Integer minprice) {
		this.minprice = minprice;
	}

	/**
	 * 查询得到的最小价格
	 */
	private Integer minprice;

	/**
	 * 查询语句及查询信息
	 * 
	 * @return
	 */
	public String getProduct() {

		Map<String, Object> cond = new HashMap<String, Object>();
		cond.put("pageSize", pageSize);
		cond.put("pageNum", pageNum);
		cond.put("product", product);
		cond.put("maxprice", maxprice);
		cond.put("minprice", minprice);

		page = ips.getProduct(cond);
		return "success";
	}
	public String delOrder() {

		int i = ips.del(product);

		if (i > 0) {
			save = "删除成功！";
		} else {
			save = "删除失败！";
		}
		return "success";
	}
	/**
	 * 商品上传
	 * 
	 * @return
	 * @throws IOException
	 */
	public String addProduct() throws IOException {

		product.setPath("../load/" + paFileName);// 把上传的图片名称放在商品信息中
		String root = ServletActionContext.getServletContext().getRealPath("/load");// 得到保存上传的路径

		InputStream is = new FileInputStream(pa);

		OutputStream os = new FileOutputStream(new File(root, paFileName));// 上传文件的地址及名字

		byte[] buffer = new byte[1024];
		int len = 0;
		while ((len = is.read(buffer)) != -1) {
			os.write(buffer, 0, len);
		}

		os.close();
		is.close();
		int i = ips.save(product);

		if (i > 0) {
			save = "增加成功！";
		} else {
			save = "增加失败！";
		}
		return "success";
	}

	public String findOrder() {

		product = ips.find(product);

		ServletActionContext.getRequest().setAttribute("product", product);
		return "success";
	}
/**
 *商品信息的修改
 *
 * @return
 * @throws IOException
 */
	public String editOrder() throws IOException {
		Product p1 = new Product();// 设置一个获取数据库中商品信息与页面返回的数据信息进行对比
		Product p2 = new Product();// 用此方法对数据库的信息进行修改
		p1 = ips.find(product);
		p2.setId(product.getId());
	
		if (!product.getName().equals(p1.getName())) {//判断数据库的值和页面传的name属性是否相同
		
			p2.setName(product.getName());
		}
		if (!product.getDescription().equals(p1.getDescription())) {//判断数据库的值和页面传的Description属性是否相同
			
			p2.setDescription(product.getDescription());
		}
		if (!product.getPrice().equals(p1.getPrice())) {//判断数据库的值和页面传的Price属性是否相同
			
			p2.setPrice(product.getPrice());
		}

		if (pa != null) {
			p2.setPath("../load/" + paFileName);// 如果文件路径发生改变时传到数据库中改变数据库的路径
			String root = ServletActionContext.getServletContext().getRealPath("/load");// 得到保存上传的路径

			InputStream is = new FileInputStream(pa);

			OutputStream os = new FileOutputStream(new File(root, paFileName));// 上传文件的地址及名字

			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = is.read(buffer)) != -1) {
				os.write(buffer, 0, len);
			}

			os.close();
			is.close();
		}
		int i =0;
		if(p2.getName()!=null||p2.getDescription()!=null||p2.getPath()!=null||p2.getPrice()!=null){//如果页面有信息发生改变调用Mapper的方法进行修改信息
			i= ips.edit(p2);	
		}
		

		if (i > 0) {
			save = "修改成功！";
		} else {
			save = "修改失败！";
		}
		return "success";
	}

	@Override
	public Product getModel() {
		// TODO Auto-generated method stub
		if (product == null) {
			product = new Product();
		}
		return product;
	}

}
